package com.blog.controller;

import com.blog.Utils.Result;
import com.blog.annotion.LoginAnnotatio;
import com.blog.base.ThreadLocalUserId;
import com.blog.service.AttentionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 任浩
 * @version 1.0
 */

@RestController
@RequestMapping("/attention")
@Slf4j
public class AttentionController {

    @Autowired
    private AttentionService attentionService;

    /**
     * 添加关注
     * @return
     */
    @PutMapping
    @LoginAnnotatio
    public Result add(@RequestParam("id") Integer id, HttpSession session){  //这个id参数是用户的id参数.可以是帖子的id参数，可以是评论的id参数。
        Integer userId = (Integer)session.getAttribute("userId");
        if(userId == null){
            userId = Integer.parseInt(ThreadLocalUserId.get());
        }
        //进行当前用户关注.
        Integer code = attentionService.addAttition(id,userId);

        return Result.success(code);
    }

    /**
     * 得到刚开始的用户的关注列表
     * @param id
     * @param session
     * @return
     */
    @GetMapping
    @LoginAnnotatio
    public Result getAttentionList(@RequestParam("id") Integer id,HttpSession session){
        //得到当前用户的id
        Integer currentuserId = (Integer)session.getAttribute("userId");
        if(currentuserId == null){
            currentuserId = Integer.parseInt(ThreadLocalUserId.get());
        }
        //查询具体的关注列表的用户信息
        Map<String, Object> map = attentionService.getAttentionList(id,currentuserId,1,10);

        return Result.success(map);
    }

    /**
     * 得到刚开始用户的粉丝列表。
     * @param id
     * @param session
     * @return
     */
    @GetMapping("/fans")
    @LoginAnnotatio
    public Result getFansList(@RequestParam("id") Integer id,HttpSession session){
        //得到当前用户的id
        Integer currentuserId = (Integer)session.getAttribute("userId");
        if(currentuserId == null){
            currentuserId = Integer.parseInt(ThreadLocalUserId.get());
        }
        Map<String, Object> map = attentionService.getFansList(id,currentuserId,1,10);

        return Result.success( map);
    }

    /**
     * 这是提高用户关注和粉丝列表的分页查询
     * @param id
     * @param page
     * @param pageNum
     * @param session
     * @param status
     * @return
     */
    @GetMapping("limit")
    @LoginAnnotatio
    public Result getLimitPage(@RequestParam("id") Integer id,@RequestParam("page") Integer page,
                                @RequestParam("pageNum") Integer pageNum,HttpSession session,
                                @RequestParam("status") Integer status){
        Map<String, Object> map = new HashMap<>();
        //得到当前用户的id
        Integer currentuserId = (Integer)session.getAttribute("userId");
        if(currentuserId == null){
            currentuserId = Integer.parseInt(ThreadLocalUserId.get());
        }

        if(status == 0) {     //代表获取用户关注列表
            //查询具体的关注列表的用户信息
            map = attentionService.getAttentionList(id,currentuserId,page,pageNum);
        }
        else {  //代表获取用户粉丝列表
            //查询具体的关注列表的用户信息
            map = attentionService.getAttentionList(id,currentuserId,1,10);
        }

        return Result.success(map);
    }
}
